<?php
use Hyang\Util;
use Hyang\Util\Crypt;
use Hyang\Util\fs;
class indexController extends \Hyang\Controller
{
    public function indexAction(){
        echo '<title>jwtai by Joshua Conero</title>';
        debug(function(&$data){
            $data = [
                'wjtai'      => '欢饮使用Wjtai框架',
                '版本信息'    => Version,
                '作者'        => 'Joshua Conero',
                '时间'        => date('Y-m-d H:i:s')
                ,'PHP_OS'      => PHP_OS
            ];
            return true;
        });
    }
    public function moreAction()
    {
        echo rand(100000,999999);
        echo rand(100000,999999);
    }
    public function constAction()
    {
        debug(get_defined_constants(true),true);
    }
    public function brxAction()
    {
        /*
        echo <<<EOD
            ddhdhdhh
        END;
        */
        //echo 'ssss';
        //debug($this,true);
        debug(function(&$data){
            return;
            $srt = serialize(rand(1000,9999));
            $data = [
                $srt,
                unserialize($srt)
            ];
        });
        // PHP 闭包函数支持
        _print(function(){
            return;//关闭
            $xhtml = '';
            $rand = rand(100000,999999);
            //$rand .= 'AJOShshbjshhh';
            $ecd = pack('H*',$rand);
            $xhtml .= $rand.' => '.$ecd;
            debug(unpack('H*',$ecd),true); 
            //$xhtml .= '<br>杨华 => '.pack('H*','杨华'); 
            $tst = function() use ($rand){
                echo '<br>';
                echo __FUNCTION__;
                echo rand(1,9);
                echo '<br>';
                return "<hr>I-Jack *".$rand.'<hr>';
            };
            echo $tst();
            return $xhtml;
        });
        // Util 类测试
        $binTest = function(){
            $str = "我知道 BRX ？ Sheのの！";
            $dec = Util::str2bin($str);
            echo '<br><h4>二进制字符</h4><textarea cols="100" rows="5">'.$dec."\r\n".base64_encode($str)."\r\n".base_convert($dec,16,2).'</textarea>';
            echo '<br><h4>原文</h4><textarea>'.Util::bin2str($dec).'</textarea>';
            //echo '<br><h4>原文</h4><textarea>'.base64_decode($dec).'</textarea>';
        };
        //$binTest();

        // crypt
        $u = new Util();
        $u::destime();
        $cryptTest = function($type=null){
            $data = 'Brximl$and$I$did$-badNew-thing；然后加上中日文呢？ の';
            $data = 'Brximl$and$I$did$-badNew-thing';
            $data = 'I born in China, Well someday I can go aborad build a life. what do you think? enHa';
            $data = file_get_contents('http://www.gzsggzyjyzx.cn/content?cls=9&id=INF-20160000859');
            $data = APP_ROOT.Debug_Dir.'source.txt';
            /*
            // 运行正常
            $m = new Crypt('My-name-is-Joshua-Conero');
            $dec = $m->encode($data);
            echo '<br><h4>数据加密</h4><textarea cols="100" rows="5">'.$dec.'</textarea>';
            echo '<br><h4>数据解密</h4><textarea cols="100" rows="5">'.$m->decode($dec).'</textarea>';
            */
            //Crypt::start('My-name-is-Joshua-Conero');
            if($type) Crypt::config(['type'=>$type]);
            Crypt::setKey(APP_ROOT.Debug_Dir.'key.rem');
            Crypt::start();
            echo '<br>获取秘钥：'.Crypt::getKey();
            $dec = Crypt::encode($data);
            //Crypt::getCipher(APP_ROOT.Debug_Dir.'encode.exe');file_put_contents(APP_ROOT.Debug_Dir.'sorce.exe',$data);
            echo '<br><h4>数据加密</h4><textarea cols="100" rows="5">'.$dec.'</textarea>';
            echo '<br><h4>数据解密</h4><textarea cols="100" rows="5">'.Crypt::decode($dec).'</textarea>';
            Crypt::end();

            $c = new Crypt(Crypt::getKey());
            //file_put_contents(APP_ROOT.Debug_Dir.'jiemi.exe',$c::decode($dec));
        };
        
        $i = 1;
        file_put_contents(APP_ROOT.Debug_Dir.'mcrypt2.txt',date('Y-m-d H:i:s')."-本次执行时间\r\n");file_put_contents(APP_ROOT.Debug_Dir.'mcrypt.txt',date('Y-m-d H:i:s')."-本次执行时间\r\n");
        foreach(mcrypt_list_algorithms("/usr/local/lib/libmcrypt") as $v){
            Util::destime();
            $cryptTest($v);
            (new fs(APP_ROOT.Debug_Dir.'mcrypt2.txt'))->append("\r\n".$v.' ( '.Util::destime(true).' )');
            $i++;
        }
        (new fs(APP_ROOT.Debug_Dir.'mcrypt2.txt'))->append("\r\n循环次数：".$i.'；总时长 '.$u::destime(true));
        
    }
    public function algoAction()
    {
        $testFunc = function(){
            $t1 = microtime(true);
            echo '<br>1:'.$t1.' , '.Util::destime();
            sleep(5);

            $t2 = microtime(true);
            echo '<br>2:'.$t2.' , '.Util::destime(true);
            echo '<hr>';
            echo '<br>3=:'.((float)$t2-(float)$t1);
        };
        $testFunc();            
    }
    public function yamlAction()
    {
        if(!extension_loaded('php_yaml')) return;
        dl(APP_ROOT.'/ext/php_yaml.dll');//  Fatal error: Call to undefined function dl()
        $p1 = function(){
            debug(yaml(APP_ROOT.Debug_Dir.'about.yaml'),true);
        };
        $p1();
    }
    public function apiAction()
    {
        //;http://www.jb51.net/article/54237.htm
        $url = 'http://www.jb51.net/article/54237.htm';
        go($url);
    }
    public function thisAction(){debug($this,true);}
    public function dbAction()
    {
        
        debug(function(&$data){
            //return;
            $opt = [
                "type"     => "oracle",//数据库类型 mysql/oracle/pgsql
                "dbname"   => "des430",
                "user"     => "SA",
                "password" => "zotion123",
                "charset"  => "utf8"
            ];
            $m = new Emma($opt);
            $data = [
                'Oracle 数据库测试'
                ,$m->query('select count(*) as "ct" from "sys_item"')
                ///,$m->query('var r refcursor;exec jy_test_sp(:r);print r;') // 存储过程
            ];
            return true;
        });
    }
    public function testAction()
    {
        // 给一个日期，输出给的日期离星期五差几天
        $test = function($dt=null){
            $dt = $dt? date_create($dt) : date_create('now');
            $week = date_format($dt,'N'); // 1 ->7
            if($week < 5) return (5 - $week);
            elseif($week > 5) return (14 - $week);
            return 0;
        };
        println($test(),$test('2017-03-19'),$test('2017-04-01'));
        // var_dump($test());
    }
}